#include "whirlixdialog.h"
#include "ui_whirlixdialog.h"

#include <QPainter>

WhirlixDialog::WhirlixDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::WhirlixDialog)
{
    ui->setupUi(this);

    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint & ~Qt::WindowMinimizeButtonHint);
    setWindowTitle(tr("Whirlix"));
    setModal(true);
    setFixedSize(343, 128);

    connect(ui->degreeSlider, SIGNAL(valueChanged(int)), ui->degreeSpinBox, SLOT(setValue(int)));
    connect(ui->degreeSpinBox, SIGNAL(valueChanged(int)), this, SLOT(emitDegreeChanged(int)));
    connect(ui->applyButton, SIGNAL(clicked()), this, SLOT(applyButtonClicked()));
    connect(ui->cancelButton, SIGNAL(clicked()), this, SLOT(cancelButtonClicked()));
}

WhirlixDialog::~WhirlixDialog()
{
    delete ui;
}

void WhirlixDialog::setDegree(int degree)
{
    ui->degreeSpinBox->setValue(degree);
}

void WhirlixDialog::emitDegreeChanged(int degree)
{
    ui->degreeSlider->setValue(degree);
    emit degreeChanged(degree);
}

void WhirlixDialog::applyButtonClicked()
{
    QString str = tr("Whirlix Filter. Degree : ")
            + QString::number(ui->degreeSpinBox->value()) + tr(".");
    emit applyOperator(str);
    close();
}

void WhirlixDialog::cancelButtonClicked()
{
    close();
}

void WhirlixDialog::closeEvent(QCloseEvent *)
{
    emit cancelOperator();
}

void WhirlixDialog::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setPen(Qt::gray);
    painter.drawRect(10, 10, width() - 20, height() - 20);
}

void WhirlixDialog::showEvent(QShowEvent *)
{
    ui->degreeSlider->setValue(0);
}
